﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TodayWhoModel;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;

namespace TodayWho
{
    public partial class user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HandleRequest();
            }
        }

        private void HandleRequest()
        {
            string userName = Request.QueryString["u"];
            if (string.IsNullOrEmpty(userName))
            {
                Response.Redirect("404.aspx");
            }
            else if(userName=="general" || userName == "music" || userName=="sport" || userName == "arts" || userName == "literature" || userName == "tech")
            {
                GetTodaysCategoryInfo(userName);
            }
            else
            {
                GetUsersInfo(userName);
            }
        }

        private void GetTodaysCategoryInfo(string category)
        {
            UserModel user = new UserModel();
            user.RetrieveTodaysUserWithCategory(category);
            SetPageTitle(user);
            SetOpenGraphMetaTags(user);
            FillInfo(user);
        }

        private void GetUsersInfo(string userName)
        {
            UserModel user = new UserModel();
            user.UserName = userName;
            user.RetrieveUserInfoWithUserName();
            SetPageTitle(user);
            SetOpenGraphMetaTags(user);
            FillInfo(user);

        }

        private void FillInfo(UserModel user)
        {
            if (user.NameSurname == null)
            {
                //errorDiv.Style["display"] = "block";
                Response.Redirect("/404.aspx");
            }
            else
            {   
                // increment pageview here // 
                PageviewModel pageview = new PageviewModel();
                pageview.UserId = user.UserId;
                pageview.UpdatePageview();
                // increment pageview end //


                imgUserPhoto.Src = user.PhotoUrl1;
                lblUserNameSurname.Text = user.NameSurname;
                lblUserShortDesc.Text = user.ShortDesc;

                if (!string.IsNullOrEmpty(user.Facebook))
                {
                    hypFacebook.HRef = "/r.aspx?r="+user.Facebook+"&u="+user.UserName+"&s=facebook";
                    hypFacebook.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Twitter))
                {
                    hypTwitter.HRef = String.Format("http://{0}/r.aspx?r=" + user.Twitter + "&u=" + user.UserName + "&s=twitter", Request.ServerVariables["SERVER_NAME"]);
                    hypTwitter.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Blogger))
                {
                    hypBlogger.HRef = "/r.aspx?r=" + user.Blogger + "&u=" + user.UserName + "&s=blogger";
                    hypBlogger.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Wordpress))
                {
                    hypWordpress.HRef = "/r.aspx?r=" + user.Wordpress + "&u=" + user.UserName + "&s=wordpress";
                    hypWordpress.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Linkedin))
                {
                    hypLinkedin.HRef = "/r.aspx?r=" + user.Linkedin + "&u=" + user.UserName + "&s=linkedin";
                    hypLinkedin.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Tumblr))
                {
                    hypTumblr.HRef = "/r.aspx?r=" + user.Tumblr + "&u=" + user.UserName + "&s=tumblr";
                    hypTumblr.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Formspring))
                {
                    hypFormspring.HRef = "/r.aspx?r=" + user.Formspring + "&u=" + user.UserName + "&s=formspring";
                    hypFormspring.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Stumbleupon))
                {
                    hypStumbleupon.HRef = "/r.aspx?r=" + user.Stumbleupon + "&u=" + user.UserName + "&s=stumbleupon";
                    hypStumbleupon.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Flickr))
                {
                    hypFlickr.HRef = "/r.aspx?r=" + user.Flickr + "&u=" + user.UserName + "&s=flickr";
                    hypFlickr.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Picasa))
                {
                    hypPicasa.HRef = "/r.aspx?r=" + user.Picasa + "&u=" + user.UserName + "&s=picasa";
                    hypPicasa.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Myspace))
                {
                    hypMyspace.HRef = "/r.aspx?r=" + user.Myspace + "&u=" + user.UserName + "&s=myspace";
                    hypMyspace.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Lastfm))
                {
                    hypLastfm.HRef = "/r.aspx?r=" + user.Lastfm + "&u=" + user.UserName + "&s=lastfm";
                    hypLastfm.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Youtube))
                {
                    hypYoutube.HRef = "/r.aspx?r=" + user.Youtube + "&u=" + user.UserName + "&s=youtube";
                    hypYoutube.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Foursquare))
                {
                    hypFoursquare.HRef = "/r.aspx?r=" + user.Foursquare + "&u=" + user.UserName + "&s=foursquare";
                    hypFoursquare.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Website1))
                {
                    hypWebsite1.HRef = "/r.aspx?r=" + user.Website1 + "&u=" + user.UserName + "&s=website1";
                    lblWebsite1.Text = user.Website1;
                    hypWebsite1.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Website2))
                {
                    hypWebsite2.HRef = "/r.aspx?r=" + user.Website2 + "&u=" + user.UserName + "&s=website2";
                    lblWebsite2.Text = user.Website2;
                    hypWebsite2.Visible = true;
                }
                if (!string.IsNullOrEmpty(user.Website3))
                {
                    hypWebsite3.HRef = "/r.aspx?r=" + user.Website3 + "&u=" + user.UserName + "&s=website3";
                    lblWebsite3.Text = user.Website3;
                    hypWebsite3.Visible = true;
                }
            }
        }

        protected void btnGoHome_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("home.aspx");
        }

        public string GetUserName()
        {
            return Request.QueryString["u"];
        }

        private void SetOpenGraphMetaTags(UserModel user)
        {
            HtmlMeta metaTitle = new HtmlMeta();
            metaTitle.Attributes.Add("property", "og:title");
            metaTitle.Attributes.Add("content", user.NameSurname + " | TodayWho");
            this.Header.Controls.Add(metaTitle);

            HtmlMeta metaType = new HtmlMeta();
            metaType.Attributes.Add("property", "og:type");
            metaType.Attributes.Add("content", "public_figure");
            this.Header.Controls.Add(metaType);

            HtmlMeta metaAdmin = new HtmlMeta();
            metaAdmin.Attributes.Add("property", "fb:admins");
            metaAdmin.Attributes.Add("content", "563383238");
            this.Header.Controls.Add(metaAdmin);

            HtmlMeta metaUrl = new HtmlMeta();
            metaUrl.Attributes.Add("property", "og:url");
            metaUrl.Attributes.Add("content", "http://todaywho.com/"+user.UserName);
            this.Header.Controls.Add(metaUrl);

            HtmlMeta metaImage = new HtmlMeta();
            metaImage.Attributes.Add("property", "og:image");
            metaImage.Attributes.Add("content", "http://todaywho.com" +user.PhotoUrl2);
            this.Header.Controls.Add(metaImage);

            HtmlMeta metaSite = new HtmlMeta();
            metaSite.Attributes.Add("property", "og:site");
            metaSite.Attributes.Add("content", "TodayWho");
            this.Header.Controls.Add(metaSite);

            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Attributes.Add("property", "og:description");
            metaDesc.Attributes.Add("content", user.ShortDesc);
            this.Header.Controls.Add(metaDesc);
        }

        private void SetPageTitle(UserModel user)
        {
            Page.Title = user.NameSurname + " | TodayWho"; 
        } 
    }
}